/**
 * FileName: EchoServer
 * Author:   powersi
 * Date:     2021/3/1 17:48
 * Description:
 */
package com.demo.zm.netty.sample.echo;

import com.demo.zm.netty.sample.echo.codec.ClientLengthDecoder;
import com.demo.zm.netty.sample.echo.codec.ClientLengthEncoder;
import com.demo.zm.netty.sample.echo.codec.ServerStringDecoder;
import com.demo.zm.netty.sample.echo.codec.ServerStringEncoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.concurrent.DefaultThreadFactory;

/**
 * Description: 
 * Author:      powersi
 * Date:        2021/3/1 17:48
 */
public class EchoServer {


    public static void main(String[] args) throws InterruptedException {

//
//        ServerBootstrap serverBootstrap = new ServerBootstrap();
//        NioEventLoopGroup group = new NioEventLoopGroup(new DefaultThreadFactory("server"));
//
//        serverBootstrap.group(group)
//                .channel(NioServerSocketChannel.class)
//                .handler(new LoggingHandler(LogLevel.DEBUG))
//                .childHandler(new ChannelInitializer<NioSocketChannel>() {
//
//                    @Override
//                    protected void initChannel(NioSocketChannel socketChannel) throws Exception {
//                        socketChannel.pipeline()
//                                .addLast(new LoggingHandler(LogLevel.DEBUG))
//                                .addLast(new EchoServerHandler())
//                                ;
//                    }
//                });
//
//        try {
//            ChannelFuture future = serverBootstrap.bind(8099).sync();
//            future.channel().closeFuture().sync();
//        } catch (Exception e){
//            e.printStackTrace();
//        } finally {
//            group.shutdownGracefully();
//        }


        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.channel(NioServerSocketChannel.class);

        serverBootstrap.handler(new LoggingHandler(LogLevel.INFO));
        NioEventLoopGroup group = new NioEventLoopGroup();
        try{
            serverBootstrap.group(group);

            serverBootstrap.childHandler(new ChannelInitializer<NioSocketChannel>() {
                @Override
                protected void initChannel(NioSocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    pipeline.addLast(new ClientLengthEncoder());
                    pipeline.addLast(new ClientLengthDecoder());
                    pipeline.addLast(new ServerStringEncoder());
                    pipeline.addLast(new ServerStringDecoder());
                    pipeline.addLast(new LoggingHandler(LogLevel.INFO));
                    pipeline.addLast(new EchoServerHandler());
                }
            });

            ChannelFuture channelFuture = serverBootstrap.bind(8099).sync();

            channelFuture.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }


    }
}
